package vn.mhg.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.mhg.user.User;
import vn.mhg.user.UserManager;

public class ControlServlet extends HttpServlet{
	public ControlServlet() {
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		System.out.println("Got request: " + req.getQueryString() + " from " + req.getRemoteAddr() +
				" (" + req.getRemoteHost() + ").");
		String action = req.getParameter("action");
		
		String sessionID = (String) req.getSession().getAttribute("FIBC_ID");
		User user = UserManager.getInstance().getUser(sessionID);
		
		if ("updatePosition".equals(action)) {
			int position = Integer.parseInt(req.getParameter("position"));
			user.setPositionSlider(position);
			System.out.print("position is updated");
		}
		if ("getPosition".equals(action)) {
			System.out.println(user.getPositionSlider());
			resp.setContentType("text/html");
			resp.setCharacterEncoding("UTF-8");
			//resp.getWriter().write("xin chao cac ban");
			resp.getWriter().write(user.getPositionSlider() +"");
			//resp.getOutputStream().write();
		}
	}
}
